package com.gt.ao.views;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class QRCodePreview extends JFrame {

    private SortedMap<Integer,byte[]> qrcodes;
    private final Image screenImage = new BufferedImage(600, 650, 2);
    ImgPanel jp;
    // ScheduledExecutorService:是从Java SE5的java.util.concurrent里，
    // 做为并发工具类被引进的，这是最理想的定时任务实现方式。
    ScheduledExecutorService service = null;
    static int qrIndex = 0;
    JLabel jLabelProcess;
    private int index_ = 0;

    public void setQrcodes(SortedMap<Integer,byte[]> qrcodes) {
        this.qrcodes = qrcodes;
    }

    public QRCodePreview() {
        //创建Frame窗口
        JFrame frame = new JFrame("请运行客户端扫描：");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jp = new ImgPanel();
        JPanel panelTop = new JPanel();
        jLabelProcess = new JLabel("0|0");
        panelTop.add(jLabelProcess);
        JLabel indexLab = new JLabel(" 第：");
        panelTop.add(indexLab);
        JTextField index = new JTextField(null, 0+"", 3);
        panelTop.add(index);
        JLabel indexLabe = new JLabel("张");
        panelTop.add(indexLabe);
        JButton ok = new JButton("暂停");
        panelTop.add(ok);
        ok.addKeyListener(new ControlTheInput());
        ok.addActionListener(x->{
            index_ = Integer.parseInt(index.getText());
            if(index_<0 || index_ >qrcodes.size()){
                JOptionPane.showMessageDialog(null,"参数不合法！","提示",JOptionPane.WARNING_MESSAGE);
                return;
            }
            if (!Objects.isNull(service)) {
                service.shutdownNow();
            }
            qrIndex = index_;
            switchImage(qrcodes.get(qrIndex));
            jLabelProcess.setText(qrIndex+"|"+qrcodes.size());
        });

        JLabel jLabelre = new JLabel("设置刷新频率：");
        panelTop.add(jLabelre);
        JTextField textField = new JTextField(null, 200 + "", 5);
        JLabel jLabel = new JLabel("ms");
        panelTop.add(textField);
        panelTop.add(jLabel);
        JButton jb = new JButton("播放");
        panelTop.add(jb);
        frame.add(panelTop, BorderLayout.NORTH);
        jb.addKeyListener(new ControlTheInput());
        jb.addActionListener(x -> {
            setInterval(Integer.parseInt(textField.getText()));
        });

        //将JPanel容器添加到JFrame容器中
        frame.add(jp);

        frame.setBackground(Color.LIGHT_GRAY);
        //设置JFrame容器的大小
        frame.setSize(600, 650);
        //在屏幕中设置显示的位置
        frame.setLocationRelativeTo(null);
        frame.setResizable(true);
        frame.setVisible(true);
        /*transBtn1.addActionListener(x->{

        });*/
    }

    private void switchImage(byte[] path) {
        jp.reloadImage(path);
    }

    public void setInterval(long fps) {
        fps = fps <= 0 ? 200 : fps;
        if (!Objects.isNull(service) && !service.isShutdown()) {
            service.shutdownNow();
            qrIndex = 0;
        }
        service = Executors.newSingleThreadScheduledExecutor();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                //System.out.println(qrcodes.get(qrIndex));
                switchImage(qrcodes.get(qrIndex++));
                jLabelProcess.setText(qrIndex+"|"+qrcodes.size());
                if (qrIndex >= qrcodes.size()) qrIndex = 0;
            }
        };
        // 第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间
        service.scheduleAtFixedRate(runnable, fps, fps, TimeUnit.MILLISECONDS);
    }

    public static void main(String[] args) {
        java.util.List<byte[]> qrcodes = new ArrayList<byte[]>(4);
        new QRCodePreview().setInterval(1000);
    }

    class ControlTheInput extends KeyAdapter {
        @Override
        public void keyTyped(KeyEvent e) {
            String key = "0123456789" + (char) 8;
            if (key.indexOf(e.getKeyChar()) < 0) {
                e.consume();//如果不是数字则取消
            }
        }
    }


}
